package asia.yuanding.wechat.web.service.impl;

import asia.yuanding.wechat.system.constant.RedisKey;
import asia.yuanding.wechat.system.exception.BusinessException;
import asia.yuanding.wechat.system.util.EmailUtil;
import asia.yuanding.wechat.system.util.R;
import asia.yuanding.wechat.system.util.RedisUtil;
import asia.yuanding.wechat.system.util.SystemUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.digest.DigestUtil;
import cn.hutool.crypto.digest.MD5;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import asia.yuanding.wechat.web.entity.User;
import asia.yuanding.wechat.web.service.UserService;
import asia.yuanding.wechat.web.mapper.UserMapper;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.mail.MessagingException;
import java.util.Date;

/**
* @author YuanDing
* @description 针对表【user】的数据库操作Service实现
* @createDate 2025-07-26 19:38:19
*/
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService{

    @Resource
    private RedisUtil redisUtil;


    @Resource
    private EmailUtil mailUtil;

    @Resource
    private SystemUtil systemUtil;

    /**
     * 用户数据脱敏
     */
    public static void userDesensitization(User user) {
        user.setPassword(null);
        user.setCreateUser(null);
        user.setUpdateTime(null);
        user.setUpdateUser(null);
        user.setCreateTime(null);
        user.setSalt(null);
        user.setStatus(null);
    }

    @Override
    public void register(User user) throws BusinessException {
        String code = user.getCode();
        String result = redisUtil.get(RedisKey.USER_TOKEN + user.getEmail());

        if (StrUtil.isBlankIfStr(result) || !result.equals(code)) {
            throw new BusinessException("验证码错误");
        }

        User one = getOne(new LambdaQueryWrapper<User>()
                .eq(User::getEmail, user.getEmail()));

        if (one != null) {
            throw new BusinessException("邮箱已注册");
        }

        String wxId = SystemUtil.getWxId();
        String salt = IdUtil.simpleUUID();
        user.setUid(IdUtil.simpleUUID());
        user.setSalt(salt);
        user.setWeChatCode(wxId);
        user.setCreateUser(user.getUid());
        user.setLastTime(new Date());

        String md5Password = DigestUtil.md5Hex(user.getPassword() + salt);
        user.setPassword(md5Password);
        boolean save = this.save(user);
        if (!save) {
            throw new BusinessException("注册失败");
        }
    }

    @Async
    @Override
    public void sendCode(String email) throws MessagingException {
        Integer code = systemUtil.getCode();
        redisUtil.set(RedisKey.USER_TOKEN + email , code.toString() , systemUtil.getCodeExpire());
        mailUtil.sendCode(email , code.toString()); //发送邮件

    }
}




